package com.budgetme.ui.fragments;

import com.budgetme.R;
import com.budgetme.database.Database;
import com.budgetme.ui.activities.BudgetDetails;
import com.budgetme.ui.activities.Details;
import com.budgetme.ui.activities.IncomeDetails;

import android.app.Activity;
import android.app.ListFragment;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

// TODO: Sorting of the list?
// TODO: Put some more info, like spent, etc, for the list items

// TODO: Look at a loader and stuff for the list activities?

public class BudgetListFragment extends ListFragment {
	private static final String TAG = "activities.fragments.BudgetListFragment";

	public static final String ARG_FILTER = "ARG_FILTER";
	
	private Activity mParentActivity;
	
	private int mFilter = Database.tbl_budgets.FILTER_ALL;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	Log.d(TAG, "onCreateView");
    	
    	// Inflate the layout for this fragment
    	View fragment = inflater.inflate(R.layout.budget_list_fragment, container, false);
    	
    	// Get the parent activity
    	mParentActivity = getActivity();
    	
		Bundle args = getArguments();
        if (args != null) {
        	mFilter = args.getInt(ARG_FILTER);
        }

		updateListAdapter();
		
		return fragment;
    
    }
	
    @Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    	menu.clear();
	    inflater.inflate(R.menu.budget_list_fragment_menu, menu);
	    super.onCreateOptionsMenu(menu, inflater);
	}
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.budget_list_menu_add:
//            	mAddBudgetSelectedListener.onAddBudgetSelected();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
	
	public void updateListAdapter() {
		
		Uri uri = Uri.withAppendedPath(Database.tbl_budgets.CONTENT_URI, "filter/" + mFilter);
		String[] projection = new String[] { Database.tbl_budgets._id, Database.tbl_budgets.t_name, Database.tbl_budgets.t_budget };
		Cursor budget_list = mParentActivity.getContentResolver().query(uri, projection, null, null, null);
		
		String[] mColumns = new String[] { Database.tbl_budgets.t_name, Database.tbl_budgets.t_budget };
		int[] mViewIds = new int[] { R.id.budget_list_name, R.id.budget_list_budget };
		
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(mParentActivity, R.layout.budget_list_contents, budget_list, mColumns, mViewIds, 0);
		
		setListAdapter(adapter);
		
	}


	

	
	// TODO: Need to handle the cursor adapter with onPause/onResume
	
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.d(TAG, "onAttach");
    }
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
		setHasOptionsMenu(true);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}
	
	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");
	}
	
	// TODO: Looks like onresume is called after onCreate, so look at optimizing the call to updateListAdapter since it gets called twice
	
	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
		updateListAdapter();
	}
	
	
	private OnListItemSelectedListener mOnListItemSelectedListener;
	private OnListItemSelectedListener mOnListItemSelectedListenerDefault = new OnListItemSelectedListener() {

		@Override
		public void onItemSelected(int id) {
			Intent starter = new Intent(mParentActivity, BudgetDetails.class);
			starter.putExtra(BudgetDetails.EXTRA_START_ID, id);
			starter.putExtra(Details.EXTRA_FRAGMENT_CLASS, BudgetDetailsFragment.class.getName());
			startActivity(starter);
		}
		
	};
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		if (mOnListItemSelectedListener == null) {
			mOnListItemSelectedListenerDefault.onItemSelected((int) id);
		} else {
			mOnListItemSelectedListener.onItemSelected((int) id);
		}
	}
	
	public void setOnListItemSelectedListener(OnListItemSelectedListener listener) {
		mOnListItemSelectedListener = listener;
	}

	public interface OnListItemSelectedListener {
		public void onItemSelected(int id);
	}
	
}





